package com.usian.controller;

import com.usian.PageResult;
import com.usian.Result;
import com.usian.feign.ItemFeign;
import com.usian.pojo.TbItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/backend/item")
public class ItemController {

    @Autowired
    private ItemFeign itemFeign;

    @RequestMapping("/selectItemInfo")
    public TbItem selectItemInfo(@RequestParam Long itemId){
        return itemFeign.selectItemInfo(itemId);
    }

    /**
     * 分页查询商品列表
     *
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/selectTbItemAllByPage")
    public Result selectTbItemAllByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows){
        PageResult pageResult = itemFeign.selectTbItemAllByPage(page, rows);
        if (pageResult.getResult().size()>0) {
            return Result.ok(pageResult);
        }
        return Result.error("查无结果");
    }

    /**
     * 添加商品
     * @param tbItem
     * @param desc
     * @param itemParams
     * @return
     */
    @RequestMapping("insertTbItem")
    public Result insertTbItem(TbItem tbItem, String desc, String itemParams){
        Integer integer = itemFeign.insertTbItem(tbItem, desc, itemParams);
        if (integer==3){
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 商品信息回显
     *
     * @param itemId
     * @return
     */
    @RequestMapping("preUpdateItem")
    private Result preUpdateItem(@RequestParam Long itemId){
        Map<String, Object> map = itemFeign.preUpdateItem(itemId);
        if (map.size()>0) {
            return Result.ok(map);
        }
        return Result.error("查询无果");
    }

    /**
     * 商品修改
     *
     * @param tbItem
     * @param desc
     * @param itemParams
     * @return
     */
    @RequestMapping("updateTbItem")
    public Result updateTbItem( TbItem tbItem, String desc, String itemParams){
        Integer integer = itemFeign.updateTbItem(tbItem, desc, itemParams);
        if (integer==3) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    @RequestMapping("deleteItemById")
    public Result deleteItemById(@RequestParam Long itemId){
        Integer integer = itemFeign.deleteItemById(itemId);
        if (integer==3) {
            return Result.ok("OK");
        }
        return Result.error("删除失败");
    }
}
